package com.example.dellpc.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.camera.CaptureActivity;
import com.example.camera.EncodingUtils;
import com.example.utils.DensityUtil;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import java.lang.ref.SoftReference;

/**
 * Created by 刘智强 on 2016/8/30.
 */
public class ZXingActivity extends Activity {
    @ViewInject(R.id.tv_result)
    private TextView tv_result;
    @ViewInject(R.id.btn_scan)
    private Button btn_scan;
    @ViewInject(R.id.btn_create)
    private Button btn_create;
    @ViewInject(R.id.iv_code)
    private ImageView iv_code;
    private static final int SCAN_CODE = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zxingactivity);
        ViewUtils.inject(this);
        init();
    }

    @OnClick({R.id.btn_scan, R.id.btn_create})
    public void onClick(Button btn) {

        switch (btn.getId()) {

            case R.id.btn_scan:
                Intent intent = new Intent(this, CaptureActivity.class);
                startActivityForResult(intent, SCAN_CODE);
                break;
            case R.id.btn_create:
                createCode();
                break;
            default:
        }
    }

    public void init() {


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == SCAN_CODE && resultCode == RESULT_OK) {
            if (data != null) {
                Bundle bundle = data.getExtras();
                String result = bundle.getString("result");
                tv_result.setText(result);
            }
        }
    }

    public void createCode() {
        int width = DensityUtil.dip2px(this, 200);
        SoftReference<Bitmap> bitmapSoftReference = new SoftReference<Bitmap>(EncodingUtils.createQRCode("http://www.baidu.com", width, width, BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)));
        iv_code.setImageBitmap(bitmapSoftReference.get());

    }
}
